package com.rongxun.hbwallet.ui.fragment;

import android.Manifest;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout;
import com.rongxun.base.MvpFragment;
import com.rongxun.hbwallet.R;
import com.rongxun.hbwallet.constants.ConfigKey;
import com.rongxun.hbwallet.constants.SPKey;
import com.rongxun.hbwallet.constants.ViewConstants;
import com.rongxun.hbwallet.model.bean.SweetsInfo;
import com.rongxun.hbwallet.model.bean.UserCandyInfo;
import com.rongxun.hbwallet.model.bean.UserCitymoneyInfo;
import com.rongxun.hbwallet.model.entity.SubCate;
import com.rongxun.hbwallet.model.bean.User;
import com.rongxun.hbwallet.mvp.contract.IMySelf;
import com.rongxun.hbwallet.mvp.model.ApiModel;
import com.rongxun.hbwallet.mvp.presenter.MySelfPresenter;
import com.rongxun.hbwallet.ui.adapter.SelfAdapter;
import com.rongxun.utils.ALog;
import com.rongxun.utils.ActivityUtils;
import com.rongxun.utils.EventConf;
import com.rongxun.utils.EventUtil;
import com.rongxun.utils.InfoEvent;
import com.rongxun.utils.JsonUtils;
import com.rongxun.utils.MyToast;
import com.rongxun.utils.PreferenceUtils;
import com.rongxun.widget.ExchangeTokenDialog;
import com.rongxun.widget.LoadingDialog;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.web3j.abi.datatypes.Int;

import java.util.List;

import butterknife.BindView;

/**
 * 创建者     庄丰泽
 * 创建时间   2018/5/21 10:28
 * 描述  我的
 */

public class MySelfFragment extends MvpFragment<IMySelf.IView, MySelfPresenter> implements IMySelf.IView{
    @BindView(R.id.myself_rv)
    RecyclerView mRecyclerView;
    @BindView(R.id.selt_TwinklingRefreshLayout)
    TwinklingRefreshLayout mTwinklingRefreshLayout;
    private SelfAdapter mAdapter;
    private int mini_exchange_Token,exchange_Token_ratio;
    private Dialog mLoadingDialog;
    private double UserCandyNum;
    @Override
    public void showLoading(boolean isShow) {
        if (isShow) {
           if(mLoadingDialog.isShowing()){
               return;
           }
            mLoadingDialog.show();
        } else {
            mLoadingDialog.dismiss();
        }
    }
    @Override
    public void showError(String msg, String errorCode) {
        MyToast.showToast(getContext(),msg);
    }
    @Override
    public void checkPermissionsSuccessful(String... permissions) {

    }
    @Override
    public void release() {
    }
    @NonNull
    @Override
    public MySelfPresenter createPresenter() {
        return new MySelfPresenter(this,new ApiModel(getContext()));
    }
    @Override
    protected int getLayoutResId() {
        return R.layout.fragment_myself;
    }

    @Override
    protected void init() {
        mLoadingDialog = LoadingDialog.createLoadingDialog(getActivity(), "加载中", false);
        getPresenter().setSelection(ViewConstants.self_lilst_title);
    }
    @Override
    protected void onLazyLoadOnce() {
        super.onLazyLoadOnce();
        getPresenter().getUserCityMoneyInfo();
        getPresenter().getUserCandyInfo();
    }

    @Override
    public void showSelection(List<SubCate> subCates) {
        mAdapter=new SelfAdapter(subCates);
        LinearLayoutManager manager=new LinearLayoutManager(activity);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(manager);
        mRecyclerView.setAdapter(mAdapter);
        showUserInfo();
    }
    @Override
    public void getUserCityMoneyInfo(UserCitymoneyInfo info) {
        if(info!=null){
            if(info.getToken()==null||info.getToken().equals("0")){
                return;
            }
            mAdapter.setCityMoney_number(info.getToken());
            mAdapter.notifyItemChanged(1);
        }
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(InfoEvent event) {
        if(event.id == EventConf.USER_CHANGE){
            showUserInfo();
            return;
        }
        if(event.id==EventConf.EXCHANGE_TOKEN){
            //获取糖果最小兑换个数和兑换比例
            getPresenter().getConfig(false,ConfigKey.CANDY_EXCHANGE_LIMIT_NUM);
            return;
        }
        if(event.id==EventConf.REFRESH_TOKEN_NUMBER){
            //更新可提币数量
            getPresenter().getUserCityMoneyInfo();
            return;
        }
    }
    @Override
    public void showUserInfo() {
      User user= JsonUtils.fromJson(PreferenceUtils.getString(SPKey.USER, ""), User.class);
        if(user!=null){
            mAdapter.setUser(user);
            mAdapter.notifyDataSetChanged();
        }
    }
    @Override
    public void getExchangeRatio(String data) {
        mini_exchange_Token=Integer.valueOf(data);//最小兑换个数
        getPresenter().getConfig(true,ConfigKey.CANDY_EXCHANGE_PROPORTION);
    }
    @Override
    public void IntoExchangeToken(String data) {
        exchange_Token_ratio=Integer.valueOf(data);//兑换比例
        ExchangeTokenDialog.Builder builder = new ExchangeTokenDialog.Builder(getContext(),UserCandyNum,mini_exchange_Token,exchange_Token_ratio,this);
        builder.create().show();
    }
    @Override
    public void showUserCandyInfo(UserCandyInfo info) {
        if(info!=null){
            UserCandyNum=info.getCandyNum();
            mAdapter.setUserCandyInfo(info);
            mAdapter.notifyItemChanged(0);
        }
    }
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventUtil.register(this);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventUtil.unregister(this);
    }
}